package alpaca.util;

import java.util.HashMap;
import java.util.Set;

/**
 * A bidirectional {@link HashMap}, both the key and value is unique and you can get 
 * both of them. The implementation is delegating two {@link HashMap} with the implementation
 * of corresponding needed methods. Both maps will always be mirrored one another.
 * 
 * @param <K>
 * @param <V>
 */

public class BiDirMap<K, V> {
	private HashMap<K, V> map = new HashMap<K, V>();
	private HashMap<V, K> reverseMap = new HashMap<V, K>();

	public K getKey(V value) { 
		return reverseMap.get(value); 
	}
	
	public V getValue(K key) { 
		return map.get(key); 
	}
	
	public boolean put(K key, V value) {
		if (!(map.containsKey(key) || map.containsValue(value))){
			reverseMap.put(value, key);
			map.put(key, value);
			return true;
		}else {
			return false;
		}
	}
	
	
	public V removeKey(K key){
		reverseMap.remove(map.get(key));
		return map.remove(key);
	}
	
	public K removeValue(V value){
		map.remove(reverseMap.get(value));
		return reverseMap.remove(value);
	}

	public boolean containsValue(V value) { 
		return reverseMap.containsKey(value); 
	}
	
	public boolean containsKey(K key){
		return map.containsKey(key); 
	}
	
	public Set<K> keySet(){
		return map.keySet();
	}
	
	public void clear(){
		map.clear();
		reverseMap.clear();
	}
}


